package com.java.category.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.java.category.mapper.CategoryMapper;
import com.java.category.service.CategoryService;
import com.java.param.ProductHotParam;
import com.java.pojo.Category;
import com.java.utils.R;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/promo/{categoryName}")
    public R byName(@PathVariable String categoryName){

        if(StringUtils.isEmpty(categoryName)){
            return R.fail("类别名称为null，无法查询名称！");
        }
        return categoryService.byName(categoryName);
    }
    @PostMapping("/hots")
    public R hotsCategory(@RequestBody @Validated ProductHotParam productHotParam, BindingResult result){
        if(result.hasErrors()){
            return R.fail("类型集合为空，无法查询！");
        }
        return categoryService.hotsCategory(productHotParam);
    }
    @GetMapping("/list")
    public R list(){
        return categoryService.list();
    }

    @GetMapping("/product/list")
    public List<Category> productList(){
        return categoryService.productList();
    }

}
